package com.yjt.zdjf.standard.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;

import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.yjt.zdjf.standard.common.constant.ConstantValues;
import com.yjt.zdjf.standard.model.zdcx.ZDCXRspMessage;

public class TcpBankServer {

	public static void main(String[] args) throws InterruptedException, IOException {
		ZDJFSerializerDeserializerForTest tool = new ZDJFSerializerDeserializerForTest();
		ServerSocket server = new ServerSocket(1730);
		
		Socket socket = server.accept();
		
		System.out.println("server socket : " + socket.getLocalPort());
		System.out.println("server socket : " + socket.getPort());
		
		InputStream is = socket.getInputStream();
		OutputStream os = socket.getOutputStream();
		
		System.out.println(tool.inputStreamToString(is));
		
		/*构造回应*/
		ZDCXRspMessage response = new ZDCXRspMessage();
		List<ZDCXRspMessage.Body.ZDXX> list = new LinkedList<ZDCXRspMessage.Body.ZDXX>();
		ZDCXRspMessage.Body.ZDXX zdxx1 = new ZDCXRspMessage.Body.ZDXX();
		zdxx1.setByzd("1");
		zdxx1.setYjzje("2");
		zdxx1.setZdh("3");
		zdxx1.setZdzq("4");
		ZDCXRspMessage.Body.ZDXX zdxx2 = new ZDCXRspMessage.Body.ZDXX();
		zdxx2.setByzd("5");
		zdxx2.setYjzje("6");
		zdxx2.setZdh("7");
		zdxx2.setZdzq("8");
		ZDCXRspMessage.Body.ZDXX zdxx3 = new ZDCXRspMessage.Body.ZDXX();
		zdxx3.setByzd("9");
		zdxx3.setYjzje("10");
		zdxx3.setZdh("11");
		zdxx3.setZdzq("12");
		list.add(zdxx1);
		list.add(zdxx2);
		list.add(zdxx3);
		response.getHead().setOuterMsgId("12344");
		response.getHead().setRecSysId("0001");
		response.getHead().setSendDate("345");
		response.getHead().setTxnType(ConstantValues.MESSAGE_HEADER_TXNTYPE_ZDCX);
		response.getBody().setZdxx(list);
		response.getBody().setByzd("13");
		response.getBody().setBz("备注字段啊啊啊啊");
		tool.serialize(response, os);
		System.out.println("=====================server send done======================");
		//TimeUnit.SECONDS.sleep(100);
	}
	
	@Test
	public void test() {
		ZDCXRspMessage response = new ZDCXRspMessage();
		List<ZDCXRspMessage.Body.ZDXX> list = new LinkedList<ZDCXRspMessage.Body.ZDXX>();
		ZDCXRspMessage.Body.ZDXX zdxx1 = new ZDCXRspMessage.Body.ZDXX();
		zdxx1.setByzd("1");
		zdxx1.setYjzje("2");
		zdxx1.setZdh("3");
		zdxx1.setZdzq("4");
		ZDCXRspMessage.Body.ZDXX zdxx2 = new ZDCXRspMessage.Body.ZDXX();
		zdxx2.setByzd("5");
		zdxx2.setYjzje("6");
		zdxx2.setZdh("7");
		zdxx2.setZdzq("8");
		ZDCXRspMessage.Body.ZDXX zdxx3 = new ZDCXRspMessage.Body.ZDXX();
		zdxx3.setByzd("9");
		zdxx3.setYjzje("10");
		zdxx3.setZdh("11");
		zdxx3.setZdzq("12");
		list.add(zdxx1);
		list.add(zdxx2);
		list.add(zdxx3);
		response.getHead().setOuterMsgId("12344");
		response.getHead().setRecSysId("0001");
		response.getHead().setSendDate("345");
		response.getHead().setTxnType(ConstantValues.MESSAGE_HEADER_TXNTYPE_ZDCX);
		response.getBody().setZdxx(list);
		response.getBody().setByzd("13");
		response.getBody().setBz("备注字段啊啊啊啊");
		System.out.println(JSON.toJSONString(response, new ValueFilter() {
			@Override
			public Object process(Object object, String name, Object value) {
				if (name.equals(ConstantValues.JSON_TAG_NAME_BODY)) {
					System.out.println(JSON.toJSONString(value));
	                return JSON.toJSONString(value);
	            }
	            return value;
			}
		}));
	}
}
